//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CallableZeroCouponBond.h"
using namespace Cephei::QL::Experimental::Callablebonds;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Instruments/Callability.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Experimental/Callablebonds/CallableFixedRateBond.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::CCallableZeroCouponBond (UInt32 settlementDays, Double faceAmount, Cephei::QL::Times::ICalendar^ calendar, DateTime maturityDate, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<Double>^ redemption, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Cephei::QL::Instruments::ICallability^>^>^ putCallSchedule, Cephei::QL::IPricingEngine^ QL_Pricer) : CCallableFixedRateBond(CCallableZeroCouponBond::typeid)
{
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    CoVector<Cephei::QL::Instruments::ICallability^>^ _CputCallSchedule;
    try
    {
#ifdef HANDLE
        _phCallableZeroCouponBond = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        QuantLib::Real _faceAmount = (QuantLib::Real)ValueHelper::Convert (faceAmount);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>::IsSome::get (paymentConvention) ? (QuantLib::BusinessDayConvention)paymentConvention->Value : QuantLib::BusinessDayConvention::Following); //10
        QuantLib::Real _redemption = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (redemption) ? (QuantLib::Real)ValueHelper::Convert (redemption->Value) : 100.0); //4
        QuantLib::Date _issueDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (issueDate) ? (QuantLib::Date)ValueHelper::Convert (issueDate->Value) : QuantLib::Date()); //4
        CCallabilityVector^ _NCputCallSchedule;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Cephei::QL::Instruments::ICallability^>^>::IsSome::get (putCallSchedule))
        {
            _CputCallSchedule = safe_cast<CoVector<Cephei::QL::Instruments::ICallability^>^> (putCallSchedule->Value);
            _CputCallSchedule->Lock();
            INativeVector<Cephei::QL::Instruments::ICallability^>^ _NCIputCallSchedule = _CputCallSchedule->getFeature (NativeFeature::shared_ptr);
            _NCputCallSchedule = safe_cast<CCallabilityVector^>(_NCIputCallSchedule);
        }
        std::vector<boost::shared_ptr<QuantLib::Callability> >& _putCallSchedule = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Cephei::QL::Instruments::ICallability^>^>::IsSome::get (putCallSchedule) ? static_cast<std::vector<boost::shared_ptr<QuantLib::Callability> >&> (_NCputCallSchedule->GetShared ()) : QuantLib::CallabilitySchedule());//2
        _ppCallableZeroCouponBond = new boost::shared_ptr<QuantLib::CallableZeroCouponBond> (new QuantLib::CallableZeroCouponBond ( _settlementDays,  _faceAmount,  _calendar,  _maturityDate,  _dayCounter,  _paymentConvention,  _redemption,  _issueDate,  _putCallSchedule ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCallableZeroCouponBond)->setPricingEngine (_QL_Pricer);
        SetCallableFixedRateBond (boost::dynamic_pointer_cast<QuantLib::CallableFixedRateBond> (*_ppCallableZeroCouponBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_CputCallSchedule != nullptr) _CputCallSchedule->Unlock();
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::CCallableZeroCouponBond (boost::shared_ptr<QuantLib::CallableZeroCouponBond>& childNative, Object^ owner) : CCallableFixedRateBond(CCallableZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phCallableZeroCouponBond = NULL;
#endif
	_ppCallableZeroCouponBond = &childNative;
    _ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (boost::dynamic_pointer_cast<QuantLib::CallableFixedRateBond> (*_ppCallableZeroCouponBond));
}
Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::CCallableZeroCouponBond (QuantLib::CallableZeroCouponBond& childNative, Object^ owner) : CCallableFixedRateBond(CCallableZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phCallableZeroCouponBond = NULL;
#endif
	_ppCallableZeroCouponBond = new boost::shared_ptr<QuantLib::CallableZeroCouponBond> (&childNative);
    _ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (boost::dynamic_pointer_cast<QuantLib::CallableFixedRateBond> (*_ppCallableZeroCouponBond));
    _CallableZeroCouponBondOwner = owner;
    _CallableFixedRateBondOwner = owner;
}

Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::CCallableZeroCouponBond (CCallableZeroCouponBond^ copy) : CCallableFixedRateBond(CCallableZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phCallableZeroCouponBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCallableZeroCouponBond = new boost::shared_ptr<QuantLib::CallableZeroCouponBond> (copy->GetShared());
        _ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (boost::dynamic_pointer_cast<QuantLib::CallableFixedRateBond> (*_ppCallableZeroCouponBond));
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::CCallableZeroCouponBond (System::Type^ t) : CCallableFixedRateBond(CCallableZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phCallableZeroCouponBond = NULL;
#endif
	if (!t->IsSubclassOf(CCallableZeroCouponBond::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::CCallableZeroCouponBond (QuantLib::Handle<QuantLib::CallableZeroCouponBond>& childNative, Object^ owner)  : CCallableFixedRateBond(CCallableZeroCouponBond::typeid)
{
	_phCallableZeroCouponBond = &childNative;
	_ppCallableZeroCouponBond = &static_cast<boost::shared_ptr<QuantLib::CallableZeroCouponBond>>(childNative.currentLink());
    _ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (boost::dynamic_pointer_cast<QuantLib::CallableFixedRateBond> (*_ppCallableZeroCouponBond));
    _CallableZeroCouponBondOwner = owner;
}
Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::CCallableZeroCouponBond (QuantLib::Handle<QuantLib::CallableZeroCouponBond> childNative)  : CCallableFixedRateBond(CCallableZeroCouponBond::typeid)
{
	_phCallableZeroCouponBond = &childNative;
	_ppCallableZeroCouponBond = &static_cast<boost::shared_ptr<QuantLib::CallableZeroCouponBond>>(childNative.currentLink());
    _ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (boost::dynamic_pointer_cast<QuantLib::CallableFixedRateBond> (*_ppCallableZeroCouponBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::CCallableZeroCouponBond (QuantLib::CallableZeroCouponBond childNative)  : CCallableFixedRateBond(CCallableZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phCallableZeroCouponBond = NULL;
#endif
	_ppCallableZeroCouponBond = new boost::shared_ptr<QuantLib::CallableZeroCouponBond> (new QuantLib::CallableZeroCouponBond (childNative));
    _ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (boost::dynamic_pointer_cast<QuantLib::CallableFixedRateBond> (*_ppCallableZeroCouponBond));
}
#endif

Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::~CCallableZeroCouponBond ()
{
    if (_ppCallableZeroCouponBond != NULL)
    {
	    delete _ppCallableZeroCouponBond;
        _ppCallableZeroCouponBond = NULL;
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::!CCallableZeroCouponBond ()
{
    if (_ppCallableZeroCouponBond != NULL)
    {
	    delete _ppCallableZeroCouponBond;
    }
}
QuantLib::CallableZeroCouponBond& Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::GetReference ()
{
    if (_ppCallableZeroCouponBond == NULL) throw gcnew NativeNullException ();
	return **_ppCallableZeroCouponBond;
}
boost::shared_ptr<QuantLib::CallableZeroCouponBond>& Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::GetShared ()
{
    if (_ppCallableZeroCouponBond == NULL) throw gcnew NativeNullException ();
	return *_ppCallableZeroCouponBond;
}
QuantLib::CallableZeroCouponBond* Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::GetPointer ()
{
    if (_ppCallableZeroCouponBond == NULL) throw gcnew NativeNullException ();
	return &**_ppCallableZeroCouponBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CallableZeroCouponBond>& Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::GetHandle ()
{
	if (_phCallableZeroCouponBond == NULL)
	{
		_phCallableZeroCouponBond = new Handle<QuantLib::CallableZeroCouponBond> (*_ppCallableZeroCouponBond);
	}
	return *_phCallableZeroCouponBond;
}
#endif
bool Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond::HasNative () 
{
	return (_ppCallableZeroCouponBond != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Callablebonds::ICallableZeroCouponBond^ Cephei::QL::Experimental::Callablebonds::CCallableZeroCouponBond_Factory::Create (UInt32 settlementDays, Double faceAmount, Cephei::QL::Times::ICalendar^ calendar, DateTime maturityDate, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<Double>^ redemption, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Cephei::QL::Instruments::ICallability^>^>^ putCallSchedule, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CCallableZeroCouponBond ( settlementDays,  faceAmount,  calendar,  maturityDate,  dayCounter,  paymentConvention,  redemption,  issueDate,  putCallSchedule,  QL_Pricer);
}
